#!/bin/sh
#
# starts/stops smartproxy.py
#
# processname: smartproxyd
# description: daemon for asynchronous calls
# chkconfig: - 90 14
#
#

# Sanity checks.
TWISTD_BINARY=`which twistd`
[ -x $TWISTD_BINARY ] || (echo "missing twistd" && exit 1)

TWISTED_NAME='smartproxy'
TWISTED_FILE='/etc/lounge/smartproxy.tac'
TWISTED_PID_DIR='/var/run/pid'
TWISTED_PID="$TWISTED_PID_DIR/smartproxyd.pid"
TWISTED_LOG='/var/log/lounge/smartproxyd.log'

USER=`id -u root`
GROUP=`id -g root`

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]
then
	. /etc/rc.d/init.d/functions
	START="daemon"
	STOP="killproc -p $TWISTED_PID -d 10 twistd"
	START="$START \"$TWISTD_BINARY -r epoll -y $TWISTED_FILE --pidfile=$TWISTED_PID --logfile=$TWISTED_LOG -u $USER -g $GROUP\""
else
	. /lib/lsb/init-functions
    START="start-stop-daemon --start --pidfile=$TWISTED_PID --exec"
    STOP="start-stop-daemon --stop --pidfile=$TWISTED_PID -n twistd"
	START="$START $TWISTD_BINARY -- -r epoll -y $TWISTED_FILE --pidfile=$TWISTED_PID --logfile=$TWISTED_LOG -u $USER -g $GROUP"
fi

start() {
    echo -n $"Starting $TWISTED_NAME: "
    [ -d $TWISTED_PID_DIR ] || mkdir -p $TWISTED_PID_DIR
	ulimit -n 32768 # increase fds - placed here for now
	$START
	RETVAL=$?
	if [ -f $TWISTED_PID ] 
	then
		chown $USER:$GROUP $TWISTED_PID
	fi
	echo
	return $RETVAL
}

stop() {
    echo -n $"Stopping $TWISTED_NAME: "
	$STOP
	RETVAL=$?
	rm -f $TWISTED_PID
	echo
	return $RETVAL
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
		status -p $TWISTED_PID $TWISTED_NAME
        RETVAL=$?
        ;;
    restart)
	stop
	start
	;;
    condrestart)
	;;
    reload)
        ;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
	;;
esac
exit $RETVAL
